package com.hj.learn.demo1;

public class Unsafebuytickets {
    public static void main(String[] args) {
        Buyticket buyticket = new Buyticket();
        new Thread(buyticket,"牛逼的我").start();
        new Thread(buyticket,"菜鸡的你").start();
        new Thread(buyticket,"垃圾黄牛").start();
    }
}

class Buyticket implements Runnable{

    private int tickets = 10;
    boolean flag = true;

    @Override
    public void run() {
        //买票
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private  void buy() throws InterruptedException {
        if (tickets<=0){
            flag = false;
            return;
        }
        Thread.sleep(100);
        System.out.println(Thread.currentThread().getName()+"拿到了票"+tickets--);
    }
}